import { LinRouter, NotFound, disableLoading } from 'lin-mizar';
import { groupRequired } from '../../middleware/jwt';
import {
  BookSearchValidator,
  CreateOrUpdateBookValidator
} from '../../validator/book';
import { PositiveIdValidator } from '../../validator/common';

import { getSafeParamId } from '../../lib/util';
import { BookNotFound } from '../../lib/exception';
import { BookDao } from '../../dao/book';
const https = require('https')
const cheerio = require('cheerio')
const fs = require('fs')
//将数据写入文件中


// book 的红图实例
const crawlerApi = new LinRouter({
  prefix: '/v1/crawler',
  module: '爬虫'
});

// book 的dao 数据库访问层实例
const bookDto = new BookDao();

crawlerApi.get('/movie', async ctx => {
  let c = ctx
  https.get('https://movie.douban.com/top250', function (res) {
    let html = ''
    res.on('data', function (chunk) {
      //console.log(chunk + '');
      html += chunk
    })

    res.on('end', function () {
      // 获取html中的数据
      const $ = cheerio.load(html)
      let allFiles = []
      //拿到每一个item中我们需要的数据
      $('li .item').each(function () {
        const title = $('.title', this).text()
        const star = $('.info .bd .rating_num', this).text()
        const pic = $('.pic img', this).attr('src')
        //数据以对象的形式存放在数组中
        allFiles.push({
          title: title,
          star: star,
          pic: pic
        })
      })
      console.log(allFiles);
      //将数据写入文件中
      fs.writeFile('./files.json', JSON.stringify(allFiles), function (err, data) {
        if (err) {
          throw err
        }
      })
    })
  })
  c.json('文件保存成功');
});

crawlerApi.get('/course', async ctx => {
  let c = ctx

  let titles = []
  let arrLength = []
  for (let i = 1; i < 20; i++) {

    (function (i) {
      setTimeout(() => {
        https.get('https://coding.imooc.com/?sort=2unlearn=0&page=' + i, function (res) {
          let html = ''
          res.on('data', function (chunk) {
            //console.log(chunk + '');
            html += chunk
          })
          res.on('end', function () {
            var $ = cheerio.load(html)
            var chapters = $('.course-list li')//以类名获取节点元素
            chapters.each(function (item) {
              var chapter = $(this)
              var chapterTitle = chapter.find('.title').text()
              var id = 'https://coding.imooc.com' + chapter.find('a').attr('href')
              titles.push({
                chapterTitle: chapterTitle,
                id: id
              })
            })
            //将数据写入文件中
            // fs.writeFile(`./files${i}.json`, JSON.stringify(titles), function (err, data) {
            //   if (err) {
            //     throw err
            //   }
            // })

          })
        })
        arrLength.push(titles.length)
        if (arrLength[i - 2] === arrLength[i - 1]) {
          fs.writeFile(`./files.json`, JSON.stringify(titles), function (err, data) {
            if (err) {
              throw err
            }
            return
          })
        }

        console.log('aaa', arrLength, i, arrLength[i - 2], arrLength[i - 1])


      }, 5000 * i);
    })(i)
  }
  c.json('文件保存成功');
});


crawlerApi.get('/baidu', async ctx => {
  let c = ctx
  console.log(ctx.req, 'aa')
  let data = await new Promise((resolve, reject) => {
    https.get('https://top.baidu.com/board?tab=realtime', function (res) {
      let html = ''
      res.on('data', function (chunk) {
        //console.log(chunk + '');
        html += chunk
      })

      res.on('end', function () {
        // 获取html中的数据
        const $ = cheerio.load(html)
        let allFiles = []
        //拿到每一个item中我们需要的数据
        $('.title_dIF3B').each(function () {
          const title = $('.c-single-text-ellipsis', this).text()
          const url = $(this).attr('href')
          //数据以对象的形式存放在数组中
          allFiles.push({
            title: title,
            url
          })
        })
        resolve(allFiles)
        //将数据写入文件中
        // fs.writeFile('./files.json', JSON.stringify(allFiles), function (err, data) {
        //   if (err) {
        //     throw err
        //   }
        // })
      })
    })
  })


  c.json(data);
});

crawlerApi.get('/gitee', async ctx => {
  let c = ctx

  let test =

    await new Promise((resolve, reject) => {
      https.get('https://gitee.com/explore/all', function (res) {
        let html = ''
        res.on('data', function (chunk) {
          html += chunk
        })
        res.on('end', function () {
          // 获取html中的数据
          const $ = cheerio.load(html)
          let allFiles = []
          console.log($('h3 a'), 'aaa')
          // 拿到每一个item中我们需要的数据
          $('h3').each(function (item, el) {
            const title = $(this).find('a').text()
            //数据以对象的形式存放在数组中
            allFiles.push({
              title: title,
              url: 'https://gitee.com' + $(this).find('a').attr('href')
            })
          })
          resolve(allFiles)
          //将数据写入文件中
          // fs.writeFile('./files.json', JSON.stringify(allFiles), function (err, data) {
          //   if (err) {
          //     throw err
          //   }
          // })
        })
      })
    })

  c.json(test);
});


crawlerApi.get('/com', async ctx => {
  let c = ctx
  let test =
    await new Promise((resolve, reject) => {
      https.get('https://www.baidu.com', function (res) {
        let html = ''
        res.on('data', function (chunk) {
          html += chunk
        })
        res.on('end', function () {
          // 获取html中的数据
          const $ = cheerio.load(html)
          resolve($('html').html())
        })
      })
    })
  ctx.type = 'html'
  c.body = `<h1>小陈</h1>` + test
});


module.exports = { crawlerApi, [disableLoading]: false };
